import config
import random
from collections.abc import Iterable
from utils import *


def register():
	return SimpleResponse()


class SimpleResponse:
	def __init__(self):
		self.type = 'passive'
		self.help = \
'''自动回复：
	用法：无（自动）
	简单的自动回复功能。'''

	def run(self, gum, msg):
		if not msg['type'] in ['FriendMessage', 'GroupMessage'] \
				or len(msg['messageChain']) <= 1 \
				or msg['messageChain'][1]['type'] != 'Plain' \
				or not msg['messageChain'][1]['text'] in config.bubbles.simpleResponse.dialogs.keys():
			return

		text = msg['messageChain'][1]['text']
		chain = MsgChain()
		res = config.bubbles.simpleResponse.dialogs[text]
		if not isinstance(res, Iterable):
			res = [res]
		chain.addPlain(random.choice(res))

		autoReply(chain, gum)